package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.DepotOutOrder;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 出库单
 */
@Component
public class DepotOutOrderDao implements BaseDao<DepotOutOrder> {
    @Override
    public int insert(DepotOutOrder entity) {
        return DBHelper.executeUpdate(
            "INSERT INTO depot_out_order (ordernumber, employee_id_apply,sum,remark) VALUES (?,?,?,?)",
            entity.getOrdernumber(), entity.getEmployee_id_apply(), entity.getSum(), entity.getRemark());
    }

    @Override
    public int update(DepotOutOrder entity) {
        return DBHelper.executeUpdate(
            "UPDATE depot_out_order SET ordernumber=?, employee_id_ratify=?, employee_id_apply=?, WHERE id=?",
            entity.getOrdernumber(), entity.getEmployee_id_ratify(), entity.getEmployee_id_apply(), entity.getId());
    }

    @Override
    public int delete(DepotOutOrder entity) {
        return DBHelper.executeUpdate("update depot_out_order set employee_id_ratify=?,is_status=9 where id=?",
            entity.getEmployee_id_ratify(), entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper
            .executeQuery("select * from depot_out_order d INNER JOIN employee e on d.employee_id_apply = e.id");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {

        DepotOutOrder depotOutOrder = (DepotOutOrder)obj[0];
        String sql =
            "select d.id,de.product_id,d.ordernumber,d.sum,d.is_status,e.realname aname, em.realname rname,d.gmt_create,d.gmt_modified,d.remark from depot_out_order d LEFT JOIN employee e on e.id = d.employee_id_apply \n"
                + "LEFT JOIN depot_out_order_details de on de.depot_out_order_id = d.id LEFT JOIN employee em on em.id = d.employee_id_ratify where d.is_deleted = 0";
        if (obj[1] != null && obj[2] != null) {
            sql += " and d.gmt_create BETWEEN '" + obj[1] + "' AND '" + obj[2] + "' ";
        }
        if (depotOutOrder.getIs_status() != 0) {
            sql += " and d.is_status = " + depotOutOrder.getIs_status();
        }
        // else {
        // sql += " and d.is_status != 9";
        // }
        if (depotOutOrder.getOrdernumber() != null && !"".equals(depotOutOrder.getOrdernumber())) {
            sql += " and concat(e.realname,em.realname,d.ordernumber) like '%" + depotOutOrder.getOrdernumber() + "%'";
        }

        sql += " GROUP BY d.id ORDER BY d.id DESC limit ?,?";

        List<Map<String, Object>> a = DBHelper.executeQuery(sql, page.getBegin(), page.getPageSize());
        return a;
    }

    @Override
    public Map<String, Object> findOne(DepotOutOrder entity) {
        return DBHelper.executeQuery("select * from depot_out_order where id=?", entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        DepotOutOrder depotOutOrder = (DepotOutOrder)obj[0];
        String sql = "select count(*) counts from depot_out_order d \n"
            + "LEFT JOIN depot_out_order_details de on de.depot_out_order_id = d.id LEFT JOIN employee e on e.id = d.employee_id_apply \n"
            + "LEFT JOIN employee em on em.id = d.employee_id_ratify where d.is_deleted=0";

        if (obj[1] != null && obj[2] != null) {
            sql += " and d.gmt_create BETWEEN '" + obj[1] + "' AND '" + obj[2] + "' ";
        }
        if (depotOutOrder.getIs_status() != 0) {
            sql += " and d.is_status = " + depotOutOrder.getIs_status();
        }
        // else {
        // sql += " and d.is_status != 9";
        // }
        if (depotOutOrder.getOrdernumber() != null && !"".equals(depotOutOrder.getOrdernumber())) {
            sql += " and concat(e.realname,em.realname,d.ordernumber) like '%" + depotOutOrder.getOrdernumber() + "%'";
        }

        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public int findMaxId() {
        return Integer
            .valueOf(DBHelper.executeQuery("select max(id) maxid from depot_out_order").get(0).get("maxid").toString());
    }

    public List<Map<String, Object>> findproduct(Object... obj) {
        return DBHelper.executeQuery(
            "select p.id,p.name,p.product_number,d.real_number from product p LEFT JOIN depot d on p.id = d.product_id where p.is_deleted=0");
    }

    public List<Map<String, Object>> findNumber(DepotOutOrder entity) {
        return DBHelper.executeQuery(
            "select p.id, o.number,real_number from depot_out_order_details o LEFT JOIN product p on o.product_id = p.id \n"
                + "LEFT JOIN depot d on d.product_id = p.id \n" + "where o.depot_out_order_id=?",
            entity.getId());
    }

    public int changeStatus(DepotOutOrder entity) {
        return DBHelper.executeUpdate("update depot_out_order set employee_id_ratify=?,is_status=1 where id=?",
            entity.getEmployee_id_ratify(), entity.getId());
    }

    public int updateReal_number(int number, String id) {
        return DBHelper.executeUpdate("update depot set real_number=? where product_id=?", number, id);
    }

}
